/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TBillDetail;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TBillDetailDao;
import com.ruipengkj.service.TBillDetailService;
/**
 * 说明：
 * 单据商品明细Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TBillDetailServiceImpl implements TBillDetailService{

	@Resource
	private TBillDetailDao tBillDetailDao;//DAO层
	
	//根据条件查询
	@Override
	public List<TBillDetail> findService(TBillDetail entity) throws Exception {
		
		String hql = "select o from TBillDetail o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getBillID()!=null&&!entity.getBillID().equals("")){
				whereHQL.append("BillID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillID()+"%");
			}
			if(entity.getBillType()!=0){
				whereHQL.append("BillType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillType()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		List<TBillDetail> list=tBillDetailDao.findDao(hql, whereHQL.toString(), param, orderby);
		return list;
	}
	
	//只查BillID
	@Override
	public List<TBillDetail> findTBillIDService(String id) throws Exception {
		String hql = "select o.BillID from TBillDetail o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (id!= null && !id.equals("")) {
			whereHQL.append("o.BillID=?");
			param.add(id);
		}		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		List<TBillDetail> list=tBillDetailDao.findDao(hql, whereHQL.toString(), param, orderby);
		return list;
	}
	
	@Override
	public List<TBillDetail> findMoneyService(TBillDetail entity) throws Exception {
		String hql = "select new TBillDetail(o.num, o.price) from TBillDetail o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getBillID()!=null&&!entity.getBillID().equals("")){
				whereHQL.append("BillID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillID()+"%");
			}
			if(entity.getBillType()!=0){
				whereHQL.append("BillType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillType()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		List<TBillDetail> list=tBillDetailDao.findDao(hql, whereHQL.toString(), param, orderby);
		return list;
	}
	
	@Override
	/**只查询商品ID*/
	public List<TBillDetail> findCommodityIDService(TBillDetail entity) throws Exception {
		String hql = "select new TBillDetail(o.commodityID, o.num, o.price) from TBillDetail o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getBillID()!=null&&!entity.getBillID().equals("")){
				whereHQL.append("o.billID=?").append(BaseQuery.AND);
				param.add(entity.getBillID());
			}
			if(entity.getBillType()!=0){
				whereHQL.append("o.billType=?").append(BaseQuery.AND);
				param.add(entity.getBillType());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		List<TBillDetail> list=tBillDetailDao.findDao(hql, whereHQL.toString(), param, orderby);
		return list;
	}
	
	//根据条件删除
	@Override
	public boolean deleteService(TBillDetail entity) throws Exception {
		String hql = "select new TBillDetail(o.detailID) from TBillDetail o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getBillType()!=0){
				whereHQL.append("BillType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillType()+"%");
			}
			if(entity.getBillID()!=null&&!entity.getBillID().equals("")){
				whereHQL.append("BillID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillID()+"%");
			}
			if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
				whereHQL.append("CommodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCommodityID()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		List<TBillDetail> list=tBillDetailDao.findDao(hql, whereHQL.toString(), param, null);
		if(list!=null&&list.size()!=0){
			for(int i = list.size()-1; i >= 0 ; --i) {
				TBillDetail com=list.get(i);
				if(com!=null){
					tBillDetailDao.deleteDao(com.getDetailID());
				}
			}
			return true;
		}
		return false;
	}

	//根据ID翻页查询
	@Override
	public PageModel<TBillDetail> getScrollService(int firstResult,
			int maxResult, TBillDetail entity) throws Exception {
		String hql = "select o from TBillDetail o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return tBillDetailDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	@Override
	public PageModel<TBillDetail> getScrollService(int firstResult,
			int maxResult, int type) throws Exception {
		String hql = "select o from TBillDetail o";
		StringBuffer whereHQL = new StringBuffer();
		
		List<Object> param = new ArrayList<Object>();
		whereHQL.append("BillType=?");
		param.add(type);
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("commodityID", BaseQuery.ASC);
		return tBillDetailDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	
	//修改
	@Override
	public void modifyService(TBillDetail entity) throws Exception {
		tBillDetailDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TBillDetail entity) throws Exception {
		tBillDetailDao.saveDao(entity);
	}

	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		tBillDetailDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TBillDetail getByIdService(String id) throws Exception {
		return tBillDetailDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TBillDetail entity) throws Exception {
		tBillDetailDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TBillDetail entity) throws Exception {
		return tBillDetailDao.saveRetDao(entity);
	}
	public TBillDetailDao gettBillDetailDao() {
		return tBillDetailDao;
	}

	public void settBillDetailDao(TBillDetailDao tBillDetailDao) {
		this.tBillDetailDao = tBillDetailDao;
	}
}
